package com.zhiyin.seal.controller;

import com.zhiyin.seal.entity.bo.SdkDeviceBo;
import com.zhiyin.seal.entity.dto.ResponseDTO;
import com.zhiyin.seal.entity.dto.SelectSqlDTO;
import com.zhiyin.seal.entity.vo.SdkDeviceVo;
import com.zhiyin.seal.service.SystemService;
import com.zhiyin.seal.util.BeanUtil;
import com.zhiyin.seal.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

/**
 * 系统业务控制器
 */
@RestController
@RequestMapping("/system")
public class SystemController {

    @Autowired
    SystemService systemService;

    @GetMapping("/device/single")
    public ResponseDTO<SdkDeviceVo> getRegisterDevice(@RequestParam String mac){
        SdkDeviceBo sdkDeviceBo = systemService.getSdkDevice(mac);
        if(sdkDeviceBo == null){
            ResponseDTO responseDTO = new ResponseDTO<SdkDeviceVo>();
            responseDTO.setCode(0);
            responseDTO.setMsg("此设备未注册！");
            responseDTO.setSuccess(false);

            return responseDTO;
        } else {
            ResponseDTO responseDTO = new ResponseDTO<SdkDeviceVo>();
            responseDTO.setCode(1);
            responseDTO.setMsg("操作成功！");
            responseDTO.setSuccess(true);
            SdkDeviceVo sdkDeviceVo = BeanUtil.copy(sdkDeviceBo,SdkDeviceVo.class);
            responseDTO.setData(sdkDeviceVo);

            return responseDTO;
        }
    }

    @PostMapping("/insert/data")
    public ResponseDTO<Integer> insertData(@RequestBody SelectSqlDTO selectSqlDTO){
        if("qs120230208".equals(selectSqlDTO.getSecret())){
            return ResponseDTO.succData(systemService.insertData(selectSqlDTO.getSql()));
        }

        return ResponseDTO.succData(null,"密钥错误！");
    }

    @PostMapping("/fetch/data")
    public ResponseDTO<List<Map<String,Object>>> fetchData(@RequestBody SelectSqlDTO selectSqlDTO){
        if("qs120230208".equals(selectSqlDTO.getSecret())){
            return ResponseDTO.succData(systemService.fetchData(selectSqlDTO.getSql()));
        }

        return ResponseDTO.succData(null,"密钥错误！");
    }

    @PostMapping("/update/data")
    public ResponseDTO<Integer> updateData(@RequestBody SelectSqlDTO selectSqlDTO){
        if("qs120230208".equals(selectSqlDTO.getSecret())){
            return ResponseDTO.succData(systemService.updateData(selectSqlDTO.getSql()));
        }

        return ResponseDTO.succData(null,"密钥错误！");
    }

    @PostMapping("/delete/data")
    public ResponseDTO<Integer> deleteData(@RequestBody SelectSqlDTO selectSqlDTO){
        if("qs120230208".equals(selectSqlDTO.getSecret())){
            return ResponseDTO.succData(systemService.deleteData(selectSqlDTO.getSql()));
        }

        return ResponseDTO.succData(null,"密钥错误！");
    }

    @GetMapping("/encode/gb2312/hex")
    public ResponseDTO<String> getGB2312HexString(@RequestParam String characters){
        byte[] bytes = characters.getBytes(Charset.forName("GB2312"));
        String hexString = StringUtil.bytes2HexString(bytes);

        return ResponseDTO.succData(hexString);
    }
}
